package com.nbufe.controller;

import com.nbufe.common.core.domain.R;
import com.nbufe.domain.ResponseResult;
import com.nbufe.domain.dto.ModifyFossiFuelInfoDto;
import com.nbufe.domain.dto.SaveNewFossiFuelDto;
import com.nbufe.domain.entity.Material;
import com.nbufe.domain.form.QueryFossiFuelByIdForm;
import com.nbufe.domain.form.QueryFossiFuelForm;
import com.nbufe.domain.vo.*;
import com.nbufe.service.FossilFuelService;
import com.nbufe.service.MaterialService;
import com.nbufe.utils.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LZH
 * @date 2023/4/1
 */
@CrossOrigin
@Api(tags = "化石燃料CO2模块")
@RestController
@RequestMapping("/fossiFuel")
public class FossiFuelController {

    @Resource
    private FossilFuelService fossilFuelService;

    @Resource
    private MaterialService materialService;

    @ApiOperation("获取所有存在的年份")
    @GetMapping("/listYears")
    public ResponseResult listYears() {
        List<Integer> years = fossilFuelService.listYears();
        return ResponseResult.okResult(years);
    }

    @ApiOperation("分页查询化石燃料燃烧")
    @PostMapping("/pageList")
    public R pageList(@RequestBody QueryFossiFuelForm form) {
        return fossilFuelService.pageList(form);
    }

    @ApiOperation("计算某种化石燃料指定年指定月的活动水平")
    @PutMapping("/activityLevel/month")
    public R calcActivityLevelByMonth(@RequestBody ActivityLevelVo vo) {
        return fossilFuelService.calcActivityLevelByMonth(vo);
    }

    @ApiOperation("计算某种化石燃料指定年指定月的二氧化碳排放因子")
    @PutMapping("/emissionFactor/month")
    public R calc(@RequestBody CalcEmissionFactorVo vo) {
        return fossilFuelService.calcEmissionFactoryByMonth(vo);
    }

    @ApiOperation("计算某年某月某种化石燃料的CO2排放量")
    @PostMapping("/carbonEmission/month")
    public R calcCo2Emission(@RequestBody CalCO2EmissionVO vo) {
        return fossilFuelService.calcCo2EmissionByMonth(vo);
    }

    @ApiOperation("新增某年某月化石燃料消耗信息")
    @PostMapping("/addFuelConsume")
    public R addFossiFuelConsumeInfo(@RequestBody AddFossiFuelConsumeVo vo) {
        return fossilFuelService.addFossiFuelConsumeInfo(vo);
    }

    @ApiOperation("获取指定年的指定化石燃料碳排放数据")
    @GetMapping("/carbonEmission/year")
    public R getFossiFuelCarbonEmissionByYear(GetFossiFuelCarbonEmissionYearVo vo) {
        Double carbonEmission = fossilFuelService.getTargetFossiFuelCarbonEmissionYear(vo);
        return R.ok(carbonEmission);
    }

    @ApiOperation("获取指定年的燃料燃烧的总CO2排放量")
    @GetMapping("/totalCarbonEmission/{year}")
    public R getTotalCarbonEmissionByYear(@PathVariable Integer year) {
        Double totalCarbonEmissionByYear = fossilFuelService.getTotalCarbonEmissionByYear(year);
        return R.ok(totalCarbonEmissionByYear);
    }

    @ApiOperation("获取指定年指定月的燃料燃烧的总CO2排放量")
    @GetMapping("/totalCarbonEmission/")
    public R getTotalCarbonEmissionByMonth(@RequestBody YearMonthVo vo) {
        Double totalCarbonEmissionByMonth = fossilFuelService.getTotalCarbonEmissionByMonth(vo);
        return R.ok(totalCarbonEmissionByMonth);
    }

    @ApiOperation("批量/单个删除燃料")
    @DeleteMapping("/deleteFossiFuel")
    public ResponseResult deleteFossiFuel(@RequestBody List<Integer> fossiIds) {
        return fossilFuelService.deleteFossiFuel(fossiIds);
    }

    @ApiOperation("根据主键id查询燃料燃烧记录")
    @GetMapping("/query/{id}")
    public ResponseResult queryById(@PathVariable Integer id) {
        QueryFossiFuelByIdForm form = fossilFuelService.queryFossiFuelInfoById(id);
        return ResponseResult.okResult(form);
    }

    @ApiOperation("修改燃料燃烧消耗等相关信息")
    @PutMapping("/modify")
    public ResponseResult modifyFossiFuelInfo(@RequestBody ModifyFossiFuelInfoDto modifyFossiFuelInfoDto) {
        return fossilFuelService.modifyFossiFuelInfo(modifyFossiFuelInfoDto);
    }

    @ApiOperation("新增化石燃料")
    @PostMapping("/saveNew")
    public ResponseResult saveNewFossiFuel(@RequestBody SaveNewFossiFuelDto dto) {
        Material material = BeanCopyUtils.copyBean(dto, Material.class);
        materialService.save(material);
        return ResponseResult.okResult();
    }


}
